<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/5/21 0021
 * Time: 18:30
 */

namespace Admin\Controller;
use Admin\Controller\ComController;
class DeptController extends ComController {
    public function index(){
        $dept = M('dept')->select();
        $this->assign('list',$dept);
        $this->assign('nav',array('user','deptlist','deptlist'));//导航
        $this -> display();
    }

    public function del(){

        $ids = isset($_POST['ids'])?$_POST['ids']:false;
        if(is_array($ids)){
//            foreach($uids as $k=>$v){
//                $uids[$k] = intval($v);
//            }
            $ids = implode(',',$ids);
            $map['id']  = array('in',$ids);
            if(M('dept')->where($map)->delete()){
                addlog('删除部门ID：'.$ids);
                $this->success('恭喜，部门删除成功！');
            }else{
                $this->error('参数错误！');
            }
        }else{
            $this->error('参数错误！');
        }
    }

    public function update(){

        $data['title'] = isset($_POST['title'])?trim($_POST['title']):false;
        $id = isset($_POST['id'])?intval($_POST['id']):false;
        if($data['title']){
            $status = isset($_POST['status'])?$_POST['status']:'';
            if($status == 'on'){
                $data['status'] =1;
            }else{
                $data['status'] =0;
            }

            if($id){
                if($group = M('dept')->where('id='.$id)->data($data)->save()){
                    addlog('编辑部门，ID：'.$id.'，名称：'.$data['title']);
                    $this->success('恭喜，部门修改成功！');
                    exit(0);
                }else{
                    $this->success('未修改内容');
                }
            }else{
                M('dept')->data($data)->add();
                addlog('新增部门，ID：'.$id.'，名称：'.$data['title']);
                $this->success('恭喜，新增部门成功！');
                exit(0);
            }
        }else{
            $this->success('部门名称不能为空！');
        }
    }

    public function edit(){

        $id = isset($_GET['id'])?intval($_GET['id']):false;
        if(!$id){
            $this->error('参数错误！');
        }

        $dept = M('dept')->where('id='.$id)->find();
        if(!$dept){
            $this->error('参数错误！');
        }
        $this->assign('dept',$dept);
        $this->assign('nav',array('user','deptlist','deptlist'));//导航
        $this -> display();
    }
}